package com.example.its201901;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView textView2;
    private ImageView imageView;
    private TextView textView;
    private TextView tvMainBalance;
    private TextView textView4;
    private Spinner spMainCarid;
    private Button btnMainQuery;
    private Button btnMainRecharge;
    private EditText edtTxtMainMoney;
    private TextView textView5;
    private TextView textView6;
   private String host="http://121.9.253.237:8888/traffic/api/v2/";
   private String pathGet=host+"get_car_account_balance.do";
   OkHttpClient okHttpClient=new OkHttpClient();
   private String pathSet=host+"get_car_account_recharge.do";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getBalanceByOkHttp(1);
        btnMainQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int carId=Integer.valueOf(spMainCarid.getSelectedItem().toString());
                getBalanceByOkHttp(carId);
            }
        });
        btnMainRecharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int carId=Integer.valueOf(spMainCarid.getSelectedItem().toString());
                int money=Integer.valueOf(edtTxtMainMoney.getText().toString());
                rechargeByOkHttp(carId,money);
                getBalanceByOkHttp(carId);
            }
        });
    }
    private void rechargeByOkHttp(int carId,int money){
        final ProgressDialog progressDialog=ProgressDialog.show(MainActivity.this,"请稍等...","账户充值中...",true);
        MediaType mediaType=MediaType.parse("application/json;charset=utf-8");
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("UserName","user1");
        jsonObject.addProperty("Money",money);
        jsonObject.addProperty("CarId",carId);
        String json=jsonObject.toString();
        Log.i("json",json);
        RequestBody requestBody=RequestBody.create(mediaType,json);
        final Request request=new Request.Builder()
                .url(pathSet)
                .post(requestBody)
                .build();
        final  Call call=okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response)throws IOException {
                final String json=response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson=new Gson();
                        Result result=gson.fromJson(json,Result.class);
                        if (result.getERRMSG().equals("成功")){
                            Toast.makeText(MainActivity.this,"充值成功",Toast.LENGTH_LONG).show();;
                        }
                        progressDialog.dismiss();
                    }
                });
                Log.i("json",json);
            }

            @Override
            public void onFailure(Call call, IOException e)  {

            }
        });

    }
    private void getBalanceByOkHttp(int carID){
        final ProgressDialog progressDialog=ProgressDialog.show(MainActivity.this,"请稍等...","获取数据中...",true);
        MediaType mediaType=MediaType.parse("application/json;charset=utf-8");
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("UserName","user1");
        jsonObject.addProperty("CarId",carID);
        String json=jsonObject.toString();
        Log.i("json",json);
        RequestBody requestBody=RequestBody.create(mediaType,json);
        Request request=new Request.Builder()
                .url(pathGet)
                .post(requestBody)
                .build();
        final Call call=okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response)throws  IOException {
                final  String json=response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson=new Gson();
                        BalanceBean balance=gson.fromJson(json,BalanceBean.class);
                        tvMainBalance.setText(String.valueOf(balance.getBalance()));
                        progressDialog.dismiss();
                    }
                });
                Log.i("json",json);
            }

            @Override
            public void onFailure(Call call, IOException e) {

            }
        });
    }

    private void initView() {
        textView2 = (TextView) findViewById(R.id.textView2);
        imageView = (ImageView) findViewById(R.id.imageView);
        textView = (TextView) findViewById(R.id.textView);
        tvMainBalance = (TextView) findViewById(R.id.tvMainBalance);
        textView4 = (TextView) findViewById(R.id.textView4);
        spMainCarid = (Spinner) findViewById(R.id.spMainCarid);
        btnMainQuery = (Button) findViewById(R.id.btnMainQuery);
        btnMainRecharge = (Button) findViewById(R.id.btnMainRecharge);
        edtTxtMainMoney = (EditText) findViewById(R.id.edtTxtMainMoney);
        textView5 = (TextView) findViewById(R.id.textView5);
        textView6 = (TextView) findViewById(R.id.textView6);
    }
}